package com.dongdongshop.service;

import com.dongdongshop.data.Page;
import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbItemCatExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class TbItemCatServiceImpl implements TbItemCatService {

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public Page selectByPid(Long pid, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        TbItemCatExample tce = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = tce.createCriteria();
        criteria.andParentIdEqualTo(pid);
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(tce);
        PageInfo info = new PageInfo(tbItemCats);
        return new Page(info.getPageNum(), info.getPageSize(),info.getPages(), info.getTotal(),info.getList());
    }

    @Override
    public boolean insertPid(TbItemCat tbItemCat) {
        int i = tbItemCatMapper.insert(tbItemCat);
        return i>0;
    }

    @Override
    public TbItemCat selectById(Long id) {
        TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(id);
        return tbItemCat;
    }

    @Override
    public boolean updatePid(TbItemCat tbItemCat) {
        int i = tbItemCatMapper.updateByPrimaryKey(tbItemCat);
        return i>0;

    }

    @Override
    public boolean deleteByIds(Long[] ids) {
        List<TbItemCat> list = tbItemCatMapper.selectByids(ids);
        int i = 0;
        if(list != null && !"".equals(list) && list.size() > 0){
            return i > 0;
        }
        i = tbItemCatMapper.deleteByids(ids);
        return i > 0;
    }

    @Override
    public List<TbItemCat> selectPid(Long pid) {
        TbItemCatExample tce = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = tce.createCriteria();
        criteria.andParentIdEqualTo(pid);
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(tce);
        return tbItemCats;

    }

}
